package com.distortiongames.level27.scenes
{
	import com.distortiongames.level27.components.KeyboardMovementComponent;
	import com.distortiongames.level27.components.render.TileRenderer;
	import com.distortiongames.level27.gameobjects.Tile;
	import com.distortiongames.level27.generation.Generator;
	import com.distortiongames.level27.generation.Map;
	import com.distortiongames.level27.generation.Point;
	import com.pblabs.core.PBGroup;
	import com.pblabs.input.KeyboardManager;
	import com.pblabs.time.ITicked;
	import com.pblabs.time.TimeManager;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Jefferson Wagner
	 */
	public class MapScene extends PBGroup implements ITicked
	{
		protected var map:Map;
		
		[Inject]
		public var stage:Stage;
		
		[Inject]
		public var timeManager:TimeManager;
		
		[Inject]
		public var keyboardManager:KeyboardManager;
		
		[Inject]
		public var canvas:Sprite;
		
		[Inject]
		public var generator:Generator;
		
		private var tiles:Vector.<Tile>;
		
		override public function initialize():void
		{
			super.initialize();
			map = generator.lastMap;
			tiles = new Vector.<Tile>();
			var start:Tile = null;
			for (var y:int = 0; y < map.height; y++)
			{
				for (var x:int = 0; x < map.width; x++)
				{
					if (map.getTile(new Point(x, y)) != Map.EMPTY)
					{
						var tile:Tile = new Tile();
						tile.owningGroup = this;
						tile.initialize();
						tile.map.map = map;
						tile.position.point = new Point(x, y);
						tile.value.value = map.getTile(new Point(x, y));
						if (tile.value.value == Map.START)
						{
							start = tile;
						}
						else if (tile.value.value >= Map.ROOM_START)
						{
							tile.renderer.room = generator.rooms[tile.value.value - Map.ROOM_START];
						}
						tiles.push(tile);
					}
				}
				
			}
			for each (tile in tiles) {
				var top:Point = tile.position.point.added(new Point(0, -1));
				var bottom:Point = tile.position.point.added(new Point(0, 1));
				var left:Point = tile.position.point.added(new Point(-1, 0));
				var right:Point = tile.position.point.added(new Point(1, 0));
				for each (var tile2:Tile in tiles) {
					if (tile2.position.point.equals(top)) {
						tile.north = tile2;
					}
					else if (tile2.position.point.equals(bottom)) {
						tile.south = tile2;
					}
					else if (tile2.position.point.equals(left)) {
						tile.west = tile2;
					}
					else if (tile2.position.point.equals(right)) {
						tile.east = tile2;
					}
				}
			}
			start.revealed = true;
			start.visible = true;
			var background:Sprite = new Sprite();
			background.graphics.beginFill(0xFFFFFF, 0);
			background.graphics.drawRect(0, 0, generator.lastMap.width * TileRenderer.WIDTH, generator.lastMap.height * TileRenderer.HEIGHT);
			canvas.addChild(background);
			timeManager.addTickedObject(this);
			
		}
		
		public function onTick():void
		{
			var spacePressed:Boolean = keyboardManager.wasKeyJustPressed(Keyboard.SPACE);
			if (spacePressed) {
				
				for each (var tile:Tile in tiles) {
					tile.visible = true;
					tile.revealed = true;
				}
			}
		}
		
		override public function destroy():void
		{
			super.destroy();
		}
	
	}

}